﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Business.Sales;
using ESOL.Common.Entity;
using ESOL.WinUI.Inv;
using ESOL.Common;
using ESOL.WinUI.Finance;

namespace ESOL.WinUI.Sales
{
    public partial class frmManageReturnToInv : BaseForm.frmBaseMdiBusinessForm
    {
        #region Vars

        #endregion

        #region Cons
        public frmManageReturnToInv()
        {
            InitializeComponent();
        }
        #endregion

        #region Func

        private string GetCondition()
        {
            string condition = "";
            if (dtpStartTime.EditValue != null)
            {
                condition += " AND B.ORDER_DATE>='" + dtpStartTime.EditValue + "'";
            }
            if (dtpEndTime.EditValue != null)
            {
                condition += " AND B.ORDER_DATE<='" + dtpEndTime.EditValue + "'";
            }
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND B.CUSTOMER_ID = '" + customer.CustomerId + "' ";
            }
            if (txtApplicationCode.Tag != null && txtApplicationCode.Tag.GetType() == typeof(SalesProdPlanApplicationEntity))
            {
                SalesProdPlanApplicationEntity prodEntity = (SalesProdPlanApplicationEntity)txtApplicationCode.Tag;
                condition += " AND B.APPLICATION_ID = '" + prodEntity.ApplicationId + "' ";
            }
            if (txtReturnOrderCode.Tag != null && txtReturnOrderCode.GetType() == typeof(SalesReturnItemEntity))
            {
                SalesReturnOrderEntity ReturnEntity = (SalesReturnOrderEntity)txtReturnOrderCode.Tag;
                condition +=" AND A.SALES_RETURN_ORDER_ID ='"+ReturnEntity.Id+"'";
            }
            if (!string.IsNullOrEmpty(txtReturnOrderCode.Text))
            {
                condition += " AND B.ORDER_CODE LIKE '%" + txtReturnOrderCode.Text + "%'";
            }
            if (!chkShowAll.Checked)
            {
                condition += "AND (((CONVERT(DECIMAL(18,2),A.RETURN_QTY))-(CONVERT(DECIMAL(18,2),isnull(E.IMPORTS_QTY,0)))) <> 0  OR G.ORDER_CODE is null)";
            }

            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "ORDER_CODE DESC";
            DataTable dt = SalesReturnOrderBs.GetReturnToInvItemsForCondition(condition, orderBy, rowCount);
            dt.Columns.Add("IS_SELECTED",typeof(Boolean));
            foreach (DataRow dr in dt.Rows)
            {
                dr["IS_SELECTED"] = false;
            }
            gvMain.DataSource = dt;

        }

        #endregion

        private void frmManageReturnToInv_Load(object sender, EventArgs e)
        {
            BindDataGrid();
        }
        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtApplicationCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductPlanApplicationQuery frm = new frmMaintainProductPlanApplicationQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtApplicationCode.Text = frm.SelectedProdPlanApplication.ApplicationCode;
                txtApplicationCode.Tag = frm.SelectedProdPlanApplication;
            }
            else
            {
                txtApplicationCode.Text = "";
                txtApplicationCode.Tag = null;
            }
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                txtCustomer.Tag = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                txtCustomer.Tag = null;
            }
        }

        protected override void Click_New()
        {
            List<SalesReturnItemEntity> mList = new List<SalesReturnItemEntity>();
            DataTable dt = gvMain.DataSource as DataTable;
            SalesReturnItemEntity entity;
            string tmpOrderId="";
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToBoolean(dr["IS_SELECTED"].ToString()) == true)
                {
                    entity = new SalesReturnItemEntity(dr);
                    if (tmpOrderId == "")
                    {
                        tmpOrderId = entity.SalesReturnOrderId;
                    }
                    else
                    {
                        if (tmpOrderId != entity.SalesReturnOrderId)
                        {
                            MessageBox.Show("所选退货单明细必须来自同一张退货单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    if (Convert.ToDecimal(dr["REMIND_QTY"].ToString()) == 0)
                    {
                        MessageBox.Show("部分退货单明细已全部入库！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    mList.Add(entity);
                }
            }
            if (mList == null || mList.Count == 0)
            {
                MessageBox.Show("请至少选择一个退货单明细！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainReturnImportOrder frm = new frmMaintainReturnImportOrder(mList);
            frm.ThisActionType = ActionType.New;
            DialogResult mdr = frm.ShowDialog();
            if (mdr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            if ((bool)dr["IS_SELECTED"])
            {
                dr["IS_SELECTED"] = false;
            }
            else
            {
                dr["IS_SELECTED"] = true;
            }
        }

        private void btnAccount_Click(object sender, EventArgs e)
        {
            List<SalesReturnItemEntity> mList = new List<SalesReturnItemEntity>();
            DataTable dt = gvMain.DataSource as DataTable;
            SalesReturnItemEntity entity=new SalesReturnItemEntity();
            string tmpOrderId = "";
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToBoolean(dr["IS_SELECTED"].ToString()) == true)
                {
                    entity = new SalesReturnItemEntity(dr);
                    SalesReturnOrderEntity returnOrder = SalesReturnOrderBs.GetSalesReturnOrderById(entity.SalesReturnOrderId);
                    if (tmpOrderId == "")
                    {
                        tmpOrderId=returnOrder.ApplicationId;
                        //tmpOrderId = entity.SalesReturnOrderId;
                    }
                    else
                    {
                        //if (tmpOrderId != entity.SalesReturnOrderId)
                        //{
                        //    MessageBox.Show("所选退货单明细必须来自同一张生产计划申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //    return;
                        //}


                        if (tmpOrderId != returnOrder.ApplicationId)
                        {
                            MessageBox.Show("所选退货单明细必须来自同一张生产计划申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                  
                    }
                    if (dr["ACCOUNT_CODE"].ToString() != "")
                    {
                        MessageBox.Show("所选退货单明细存在已产生退货账款单的单据！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    mList.Add(entity);
                }
            }
            if (mList == null || mList.Count == 0)
            {
                MessageBox.Show("请至少选择一个退货单明细！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainReturnAccountOrder frm = new frmMaintainReturnAccountOrder();
            frm.SelectedReturnItems = mList;
            frm.ThisActionType = ActionType.New;
            DialogResult mdr = frm.ShowDialog();
            if (mdr == DialogResult.OK)
            {
                BindDataGrid();
            }
            // DialogResult drt = MessageBox.Show("确认处理？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //if (drt == DialogResult.No)
            //{
            //    return;
            //}
            //try
            //{
            //    string msg="";
            //    bool successful = SalesReturnOrderBs.ReturnAccount(mList,msg);
            //    if (successful)
            //    {
            //        MessageBox.Show("处理成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        BindDataGrid();
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
        }
    }
}